package com.shuoyue.ycgk.ui.mine.mypaperset;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.ui.papergroups.adapter.PaperSetAdapter;
import com.shuoyue.ycgk.ui.papergroups.groupcommon.PaperCommonInfoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyPaperGroupActivity extends BaseMvpActivity<MyPaperContract.Presenter>
        implements MyPaperContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    /**
     * @param context
     * @param module  收藏分模块，购买不分模块传0
     */
    public static void start(Context context, int module) {
        context.startActivity(new Intent(context, MyPaperGroupActivity.class)
                .putExtra("module", module));
    }


    PaperSetAdapter paperSetAdapter;

    int module = 0;
    ListWithPage<PaperSet> listWithPage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sigle_list;
    }

    @Override
    protected void initView() {
        module = getIntent().getIntExtra("module", 0);
        if (module == 0) {
            pageTitle.setText("我的题库");
        } else {
            pageTitle.setText("题库收藏");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMyPaperSet(module, 1);
            refreshLayout.finishRefresh();
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getMyPaperSet(module, listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore();
        });
        paperSetAdapter = new PaperSetAdapter(null);
        recyclerView.setAdapter(paperSetAdapter);
        paperSetAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            PaperSet paperSet = (PaperSet) adapter.getItem(position);
            PaperCommonInfoActivity.start(mContext, paperSet.getId(), -1, -1);
        });
        registEmptyView(paperSetAdapter, "暂未购买习题包");
    }


    @Override
    public void initData() {
        mPresenter = new MyPaperContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getMyPaperSet(module, 1);
    }


    @Override
    public void setPaperSet(ListWithPage<PaperSet> paperSets) {
        listWithPage = paperSets;
        if (paperSets.getCurrent() == 1) {
            paperSetAdapter.resetData(paperSets.getRecords());
        } else {
            paperSetAdapter.addData(paperSets.getRecords());
        }
        refreshlayout.setEnableLoadMore(paperSets.getCurrent() != paperSets.getPages() && paperSets.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
